package sk.upjs.ics.fca;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.junit.Test;

import sk.upjs.ics.fca.bonds.application.AlmightyAlgorithm;

/**
 * Tento JUnit test testuje vygenerovane kombinacie fuzzy kontextov pre nas
 * modelovy priklad
 * 
 * @author Patrik Mihalcin
 * 
 */
public class AlmightyAlgorithmDataGenerator {

	private String pathToGlobalContext = "";
	private String pathToInputContexts = "";

	@Test
	public void test() {
		double fillRatio1 = 0.25;
		double fillRatio2 = 0.25;

		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 4; j++) {
				pathToGlobalContext = "data/globalContextFillRatios"
						+ fillRatio1 + "_" + fillRatio2 + ".dat";
				pathToInputContexts = "data/inputContextsFillRatios"
						+ fillRatio1 + "_" + fillRatio2 + ".dat";

				generateInputContextsData(fillRatio1);
				generateGlobalContextData(fillRatio2);

				AlmightyAlgorithm a = new AlmightyAlgorithm(new File(
						pathToGlobalContext), new File(pathToInputContexts), 3);
				// execute algorithm
				a.execute();

				fillRatio2 += 0.25;
			}
			fillRatio2 = 0.25;
			fillRatio1 += 0.25;
		}
	}

	public void generateInputContextsData(double fillRatio) {

		PrintWriter writer = null;
		try {
			writer = new PrintWriter(pathToInputContexts, "UTF-8");
			// object contexts
			for (int i = 1; i <= 3; i++) {
				writer.println("context o " + i);
				Generator g = new Generator(2, 3, fillRatio);
				g.generateRelation();
				List<List<Double>> relation = g.getRelation();
				for (List<Double> row : relation) {
					for (Double value : row) {
						writer.print(value + " ");
					}
					writer.println();
				}
			}

			// attribute contexts
			for (int i = 1; i <= 3; i++) {
				writer.println("context a " + i);
				Generator g = new Generator(3, 2, fillRatio);
				g.generateRelation();
				List<List<Double>> relation = g.getRelation();
				for (List<Double> row : relation) {
					for (Double value : row) {
						writer.print(value + " ");
					}
					writer.println();
				}
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} finally {
			writer.close();
		}
	}

	public void generateGlobalContextData(double fillRatio) {
		PrintWriter writer = null;
		try {
			writer = new PrintWriter(pathToGlobalContext, "UTF-8");
			for (int i = 1; i <= 3; i++) {
				for (int j = 1; j <= 3; j++) {
					writer.println("context o " + i + " a " + j);
					Generator g = new Generator(2, 2, fillRatio);
					g.generateRelation();
					List<List<Double>> relation = g.getRelation();
					for (List<Double> row : relation) {
						for (Double value : row) {
							writer.print(value + " ");
						}
						writer.println();
					}
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} finally {
			writer.close();
		}
	}
}
